﻿using UnityEngine;
using ProtoBuf.Message;
using System.Collections.Generic;

namespace UI
{
    public class OnSailItem : MonoBehaviour
    {
        public UILabel labDiscount;
        public UILabel labTitle;
        public UILabel labLeftBuyTimes;
        public GameObject discountRoot;
        public GameObject btnBuy;
        public GameObject buyed;
        public UISprite costLogo;
        public UILabel labPrice;
        public UIScrollView scrollView;
        public SimpleGrid simpleGrid;
        public GameObject itemPref;

        public GameObject middle;
        public UILabel getNum;
        public UILabel getName;
        public UISprite titleBg;
        public delegate void UpdateList(List<ActivityShopItem> list);
        public UpdateList m_update;

        ActivityShopItem rewardInfo;
        int itemIndex;
        string activityId;       

        public void SetReward(ActivityShopItem item,string acId)//设置普通多个商品的购买
        {
            rewardInfo = item;
            activityId = acId;

            float disc = CommonMethod.ParseDiscount(item.discount);
            if (disc > 0 && disc < 10)
            {
                discountRoot.SetActive(true);
                labDiscount.text = disc.ToString();
            }
            else
            {
                discountRoot.SetActive(false);
            }

            labTitle.text = item.name;
            labLeftBuyTimes.text = item.left.ToString() + "/" + item.total.ToString();
            //购买状态
            btnBuy.SetActive(false);
            buyed.SetActive(false);
            costLogo.gameObject.SetActive(false);
            labPrice.gameObject.SetActive(false);

            if (item.status == 2)
                buyed.SetActive(true);
            else
            {
                costLogo.gameObject.SetActive(true);
                labPrice.gameObject.SetActive(true);
                labPrice.text = item.costResource[0].num.ToString();
                btnBuy.SetActive(true);
            }

            //礼包物品
            foreach (var c in simpleGrid.childs)
                Destroy(c);
            simpleGrid.childs.Clear();

            foreach (var i in item.getResource)
            {
                GameObject obj = Instantiate(itemPref);
                obj.SetActive(true);
                obj.GetComponent<CommonIcon>().SetData(i.id, true, i.num);
                obj.transform.FindChild("ItemName").GetComponent<UILabel>().text = TableTextBlo.GetText(CommonMethod.GetObjectNameTextId(i.id));//Text里面没有==
                simpleGrid.AddChild(obj);
                obj.transform.localScale = Vector3.one * 0.8f;
            }
            scrollView.ResetPosition();
        }

        public void SetSingleReward(ActivityShopItem item, string acId)//设置普通单个商品的购买
        {
            rewardInfo = item;
            activityId = acId;

            float disc = CommonMethod.ParseDiscount(item.discount);
            if (disc > 0 && disc < 10)
            {
                discountRoot.SetActive(true);
                labDiscount.text = disc.ToString();
            }
            else
            {
                discountRoot.SetActive(false);
            }
            labLeftBuyTimes.text = item.left.ToString() + "/" + item.total.ToString();
            //购买状态
            btnBuy.SetActive(false);
            buyed.SetActive(false);
            costLogo.gameObject.SetActive(false);
            labPrice.gameObject.SetActive(false);
            if (item.status == 2)
                buyed.SetActive(true);
            else
            {
                costLogo.gameObject.SetActive(true);
                labPrice.gameObject.SetActive(true);
                labPrice.text = item.costResource[0].num.ToString();
                btnBuy.SetActive(true);
            }

            itemPref.SetActive(true);
            itemPref.GetComponent<CommonIcon>().SetData(item.getResource[0].id, true);
            getNum.text = item.getResource[0].num.ToString();
            getName.text = TableTextBlo.GetText(CommonMethod.GetObjectNameTextId(item.getResource[0].id));
            labTitle.text = string.Format(TableTextBlo.GetText("activity_buy_Times_Limit"), item.total);
        }

        public void SetItem(ActivityShopItem item, string acId)//设置限时抢购/兑换物品
        {
            rewardInfo = item;
            activityId = acId;

            float disc = CommonMethod.ParseDiscount(item.discount);
            if (disc > 0 && disc < 10)
            {
                discountRoot.SetActive(true);
                labDiscount.text = disc.ToString();
                titleBg.spriteName = "UI_activity_tiao";
                titleBg.transform.localPosition += new Vector3(0, 3, 0);
            }
            else
            {
                discountRoot.SetActive(false);
                titleBg.spriteName = "UI_activity_zhishitiao";
                labTitle.transform.localPosition -= new Vector3(30,0,0);
            }
         
            labTitle.text = string.Format(TableTextBlo.GetText("Time_Limit_Change_Num_Limit"), item.name,item.total) ;
            labLeftBuyTimes.text = item.left.ToString() + "/" + item.total.ToString();
            //购买状态
            btnBuy.SetActive(false);
            buyed.SetActive(false);

            if (item.status == 2)
                buyed.SetActive(true);
            else
                btnBuy.SetActive(true);

            //礼包物品
            foreach (var c in simpleGrid.childs)
                Destroy(c);
            simpleGrid.childs.Clear();
            itemPref.SetActive(true);
            foreach (var i in item.costResource)
            {
                GameObject obj = Instantiate(itemPref);
                obj.GetComponent<CommonIcon>().SetData(i.id, true, i.num);
                obj.transform.FindChild("ItemName").GetComponent<UILabel>().text = TableTextBlo.GetText(CommonMethod.GetObjectNameTextId(i.id));
                simpleGrid.AddChild(obj);
                obj.transform.localScale = Vector3.one * 0.8f;
            }
            simpleGrid.AddChild(middle);

            SimpleGrid simpleChild = simpleGrid.transform.Find("simpleChild").GetComponent<SimpleGrid>();
            foreach (var c in simpleChild.childs)
                Destroy(c);
            simpleChild.childs.Clear();
            foreach (var i in item.getResource)
            {
                GameObject obj = Instantiate(itemPref);
                obj.GetComponent<CommonIcon>().SetData(i.id, true, i.num);
                obj.transform.FindChild("ItemName").GetComponent<UILabel>().text = TableTextBlo.GetText(CommonMethod.GetObjectNameTextId(i.id));
                simpleChild.AddChild(obj);
                obj.transform.localScale = Vector3.one * 0.8f;
            }
            simpleGrid.AddChild(simpleChild.gameObject);
            itemPref.SetActive(false);
            scrollView.ResetPosition();
        }

        public void OnBuyBtn()
        {
            foreach(var cost in rewardInfo.costResource)
            {
                if (cost.id == Dict.Config.TableConst.diamond)
                {
                    if (StaticData.playerData.diamond < cost.num)
                    {
                        BoxManager.CreatePopupTextBox(TableTextBlo.GetText("diamond_lack"));
                        return;
                    }
                    else if (rewardInfo.status == 1)
                    {
                        //购买确认弹窗
                        var str = string.Format(TableTextBlo.GetText("shop_diamond_buy_sure"), cost.num);
                        BoxManager.CreateMessageBoxWithCofirmAndCancelButton(str, true, (b) =>
                        {
                            if (b)
                            {
                                BuyItem();
                            }
                        }, TableTextBlo.GetText("shop_buy_sure"));
                        return;
                    }
                }
            }

            if(rewardInfo.status == 0)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("Resource_lack"));
                return;
            }
            BuyItem();
        }

        void BuyItem()
        {
            if (rewardInfo.pickOneOrGetAll == 1)
            {
                //全部
                CSGetActivityShopReward msg = new CSGetActivityShopReward();
                foreach (var i in rewardInfo.costResource)
                    msg.costResources.Add(i);
                foreach (var i in rewardInfo.getResource)
                    msg.getResources.Add(i);
                msg.index = rewardInfo.index;
                msg.activityId = activityId;
                NetAPI.SendHttp(OpDefine.CSGetActivityShopReward, msg, OnGetRewardRecv);
            }
            else
            {
                //x选1
                PageManager.Instance.OpenPage("SelectAcRewardPage", "");
                SelectAcRewardPage reward = PageManager.Instance.GetDictPage("SelectAcRewardPage") as SelectAcRewardPage;
                reward.m_click = this.SelectAcAward;
                reward.SetReward(rewardInfo.getResource);
            }
        }

        void SelectAcAward(BaseResource res)
        {
            CSGetActivityShopReward msg = new CSGetActivityShopReward();
            msg.getResources.Add(res);
            foreach (var i in rewardInfo.costResource)
                msg.costResources.Add(i);
            msg.index = rewardInfo.index;
            msg.activityId = activityId;
            NetAPI.SendHttp(OpDefine.CSGetActivityShopReward, msg, OnGetRewardRecv);
        }

        void OnGetRewardRecv(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                SCGetActivityShopReward msg = packet.kBody as SCGetActivityShopReward;
                StaticData.playerData.AddResultObj(msg.get);
                StaticData.playerData.Delete(msg.costResources);
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("buy_success"));
                CommonMethod.ShowGetObjs(msg.get, "", false);
                m_update(msg.shopItemList);
            }
        }
    }
}

